import moment from 'moment';
import db from './db';
import AutoSaverConfig from '@/models/AutoSaverConfig';
moment.locale('zh-cn');
export class AutoSaver {
    public config: AutoSaverConfig;
    public running: boolean = false;
    private currentInterval: any = null;
    private saving: boolean = false;
    constructor(c: AutoSaverConfig) {
        this.config = c;
        if (this.config) {
            if (this.config.enable) {
                this.currentInterval = setInterval(() => {
                    this.saveAction();
                }, this.config.ms);
                this.running = true;
                console.log("自动备份任务已经开始")
            }
        }
    }
    public saveAction() {
        if (this.saving) {
            return;
        }
        this.saving = true;
        db.slientSave(this.config.savePath).then(() => {
            console.log("自动保存成功")
            this.saving = false;
        }).catch(reason => {
            console.error(reason.message);
        })
    }
    public stop() {
        if (this.currentInterval) {
            clearInterval(this.currentInterval);
            this.running = false;
        }
    }
}